import { defHttp } from '/@/utils/http/axios';
import { User } from '/@/api/monitor/entity/entity';
import { ErrorMessageMode } from '/#/axios';
import { StdResponse } from '/@/api/model/baseModel';

/**
 * @description: Login interface parameters
 */
export interface LoginParams {
  username: string;
  password: string;
}

/**
 * @description: Register interface parameters
 */
export interface RegisterParams {
  username: string;
  password: string;
  email: string;
}

/**
 * @description: getUserInfoById
 */
export function getUserSelfInfo(): Promise<User> {
  return defHttp.get<User>({ url: '/user/info' });
}

/**
 * @description: user login api
 */
export function passwordLogin(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<string>(
    {
      url: '/auth/login',
      params,
      transformResponse: (data: any) => {
        const resp: StdResponse = JSON.parse(data);
        return resp;
      },
    },
    { errorMessageMode: mode },
  );
}

/**
 * @description: register
 */
export function register(params: RegisterParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<string>(
    {
      url: '/auth/register',
      data: params,
      transformResponse: (data: any) => {
        const resp: StdResponse = JSON.parse(data);
        return resp;
      },
    },
    { errorMessageMode: mode },
  );
}
